﻿using UnityEngine;
using System.Collections;

public class Monkey : MonoBehaviour {

	public float speedMovement = 2f;
	public float speedEvasion = 1;
	public int pontos = 50;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey (KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
			this.rotateRight();
		}
		if(Input.GetKey(KeyCode.D) || Input.GetKey (KeyCode.RightArrow)){
			this.rotateLeft();
		}

		Input.gyro.enabled = true;

		this.keepClimbing();
		this.checkTouchDetection();
		this.checkTiltDetection ();

		//this.transform.rotation = Input.gyro.attitude;

	}

	private void checkTiltDetection(){
		Vector3 dir = Vector3.zero;
		dir.z = -Input.acceleration.y * this.speedMovement;
		this.transform.Rotate (new Vector3 (0, 0, dir.z));
		//transform.Translate(dir.x,0,0);
	}
	
	private void rotateRight(){
		this.transform.Rotate(new Vector3(0,0,this.speedEvasion));
	}

	private void rotateLeft(){
		this.transform.Rotate(new Vector3(0,0,this.speedEvasion * -1));
	}

	private void checkTouchDetection(){
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
			Vector3 touchPosition = Input.GetTouch(0).position;
			float halfScreen = Screen.width / 2;
			if(touchPosition.x > halfScreen){
				this.rotateLeft();
			}
			else{
				this.rotateRight();
			}
		}

	}

	private void keepClimbing(){
		this.transform.Translate (Vector3.forward * (Time.deltaTime * this.speedMovement));
	}

	void OnGUI()
	{
		GUI.Label (new Rect (0, 20, 80, 30),"<color=green>Pontos</color> : " + "<color=black>"+pontos+"</color>");
		GUI.Label (new Rect (0, 50, 100, 50),"<color=green>Distancia</color> : " + "<color=black>"+(int)this.transform.localPosition.z*10+"</color>");
		//GUI.Label (new Rect (0, 80, 500, 200), "<color=black>"+Input.gyro.attitude.ToString ()+"</color>");
	}



}
